package com.hava.demo.springmvc.onlyone.service;

import com.hava.demo.springmvc.onlyone.entity.OnlyOne;
import com.hava.demo.springmvc.onlyone.repository.OnlyOneRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.List;
import java.util.Map;

/**
 * Created by zhanpeng on 15-2-11.
 */
@Service
@Transactional
public class OnlyOneService {

    //这里必须添加注释，否则会出现NullPointerException
    @Autowired
    private OnlyOneRepository one;
    
    public void createOnlyOne(OnlyOne entity)
    {
        one.save(entity);
    }
    
    public void deleteOnlyOne(Long id)
    {
        one.delete(id);
    }
    
    public void updateOnlyOne(OnlyOne entity)
    {
        one.save(entity);
    }
    
    public OnlyOne getOnlyOne(Long id)
    {
        return one.findOne(id);
    }
    
    public List<OnlyOne> getAllOnlyOne()
    {
        return (List<OnlyOne>)one.findAll();
    }
    
    public Page<OnlyOne> searchOnlyOnePage(Map<String,Object> searchParams,int pageNumber,int pageSize,String sortType)
    {
        System.out.println("searchParams:" + searchParams);
        
        PageRequest pageRequest = buildPageRequest(pageNumber,pageSize,sortType);
        //Specification<One> specification = buildSpecification
        return one.findAll(pageRequest);
    }
    
    private PageRequest buildPageRequest(int pageNumber,int pageSize,String sortType)
    {
        Sort sort = null;
        if("auto".equals(sortType))
        {
            sort = new Sort(Sort.Direction.DESC,"id");
        }
        else if("title".equals(sortType))
        {
            sort = new Sort(Sort.Direction.ASC,"title");
        }
        
        return new PageRequest(pageNumber - 1,pageSize,sort);
    }
    
    private Specification<OnlyOne> buildSpecification()
    {
        Specification<OnlyOne> specification = new Specification<OnlyOne>()
        {
            @Override
            public Predicate toPredicate(Root<OnlyOne> root, CriteriaQuery<?> query, CriteriaBuilder cb)
            {
                //path转换
                Path<String> idPath = root.get("id");
                return null;
            }
        };
        return specification;
    }
    
//    private Specification<One> buildSpecification(Long id,Map<String,Object> searchParams)
//
//    private Specification<Task> buildSpecification(Long userId, Map<String, Object> searchParams) {
//        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
//        filters.put("user.id", new SearchFilter("user.id", Operator.EQ, userId));
//        Specification<Task> spec = DynamicSpecifications.bySearchFilter(filters.values(), Task.class);
//        return spec;
//    }
    
}
